#ifndef _CEVENTSYSTEM_H_
#define _CEVENTSYSTEM_H_

////////////////////////////////////////////////////////////
//	Filename:			CEventSystem.h
//
//	Date Created:		8/27/2011
//	File Creator:		Steven Broyles
//
//	Last Modified:		8/27/2011	
//	Modifier Initials:	SB
//
//	Description:		This is the base interface class for
//						event listeners, allowing classes that 
//						inherit from it the functionality needed 
//						to handle events.
////////////////////////////////////////////////////////////
#include "..\PrecompiledHeader.h"

#include "CEvent.h"

class IListener;

enum EVENTPRIORITY { EVNT_LOW = 0, EVNT_MED, EVNT_HIGH, EVNT_URGENT };
////
//EVENTPRIORITY
//
//EVNT_LOW
// this is be among the last events send when process events is called
//
//EVNT_MED
// this event will be sent after events with high priorities, and before
// events will low priorities
//
//EVNT_HIGH
//	among the first event send from the process events call
//
//EVNT_URGENT
//  this event is sent immediately 
////

class CEventSystem
{
	map<int, vector<IListener*>>	m_mClients; //The database of Events and Clients registered to accept them
	vector<CEvent>		m_vCurrentEvents;	//The current list of events that have no been processed
	vector<CEvent>		m_vSafeEvents; //The list of events to be processed later 
	//safe events ignore events with urgent priorities

	CEvent m_cUrgentEvent;

	void DispatchEvent( CEvent* _pcEvent );
	bool AlreadyRegistered( EVENTID _nEventID, IListener* _pcClient );

	//Constructor
	CEventSystem(  );
	
	//Destructor
	~CEventSystem(  );

	DISALLOW_COPY_AND_ASSIGN( CEventSystem );
public:

	////////////////////////////////////////////////////////////
	//	Name:				GetInstance
	//	Description:		Returns the singleton instance of the
	//						EventSystem
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			CEventSystem*
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	static CEventSystem* GetInstance( void );

	////////////////////////////////////////////////////////////
	//	Name:				RegisterClient
	//	Description:		Registers the client to receive events. 
	//
	//	Ins:				_nEventID
	//						_pcClient
	//	
	//	Outs:				none
	//
	//	Returns:			void
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void RegisterClient( EVENTID _nEventID, IListener* _pcClient );

	////////////////////////////////////////////////////////////
	//	Name:				UnregisterClient
	//	Description:		Unregisters a single event from the specified client
	//
	//	Ins:				_nEventID
	//						_pcClient
	//	
	//	Outs:				none
	//
	//	Returns:			CEventSystem*
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void UnregisterClient( EVENTID _nEventID, IListener* _pcClient );

	////////////////////////////////////////////////////////////
	//	Name:				UnregisterClientAll
	//	Description:		Completely Unregisters a client from the Event System
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			CEventSystem*
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void UnregisterClientAll( IListener* _pcClient );

	////////////////////////////////////////////////////////////
	//	Name:				SendEvent
	//	Description:		Prepares an event to be sent to every 
	//						listener listing for it.
	//
	//	Ins:				_nEventID,
	//						_pData
	//	
	//	Outs:				none
	//
	//	Returns:			void*
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void SendEvent( EVENTID _nEventID, void* _pData = NULL, int _nPriority = EVNT_LOW );

	////////////////////////////////////////////////////////////
	//	Name:				SendSafeEvent
	//	Description:		Send an event with an event or function
	//						called by an event for the next process
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			void
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void SendSafeEvent( EVENTID _nEventID, void* _pData = NULL, int _nPriority = EVNT_LOW );

	////////////////////////////////////////////////////////////
	//	Name:				ProcessEvents
	//	Description:		Dispatches all events being stored by Event System.
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			void
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void ProcessEvents( void );

	////////////////////////////////////////////////////////////
	//	Name:				ClearEvents
	//	Description:		Removes events that haven't been processed.
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			void
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void ClearEvents( void );

	////////////////////////////////////////////////////////////
	//	Name:				Shutdown
	//	Description:		Shutdowns the Event System.
	//
	//	Ins:				none
	//	
	//	Outs:				none
	//
	//	Returns:			void
	//
	//	Last Modified:		8/27/2011	
	//	Modifier Initials:	SB
	//
	//	Known Bugs:			
	////////////////////////////////////////////////////////////
	void Shutdown( void );
};

#endif